
import { getServicerId } from "@/server";
import { useRequest } from 'ahooks'
import { useDispatch } from "react-redux";
import { setServiceInfo, ServiveInfoType } from "@/store/ServiceInfoStore";
import { MessageType,setMessageList } from "@/store/MessageStore";
import timeFormater from "time-formater";

const useLoadServeInfo = (type = null, cb: any = null,scrolltoBottom: any = null) => {
    const dispatch = useDispatch()
    const { loading } = useRequest(() => {
        return getServicerId(type)
    }, {
        onSuccess(res: ServiveInfoType) {
            console.log('useLoadServeInfo',type,res)
            if (type === null && res.chatid.includes('AI')) {
                
                const { chatid, nickname, head, prologue } = res
                const prologueArr = prologue!.split('\n')
                setTimeout(() => {
                    const PrologueMsgText: MessageType = {
                        id: new Date().getTime() + '_local',
                        head,
                        nickname,
                        sendid: chatid,
                        time: timeFormater().format('YYYY-MM-DD HH:mm:ss'),
                        isdelete: 'null',
                        info: {
                            type: 'text',
                            value: prologueArr[0]
                        }
                    }
                    dispatch(setMessageList(PrologueMsgText))

                    
                   
                }, 0)
                const PrologueMsgTips: MessageType = {
                    id: new Date().getTime() + '_local',
                    head,
                    nickname,
                    sendid: chatid,
                    time: timeFormater().format('YYYY-MM-DD HH:mm:ss'),
                    info: {
                        type: 'Prologue',
                        value: prologue!
                    }
                }
                dispatch(setMessageList(PrologueMsgTips))
                scrolltoBottom && scrolltoBottom()
               
            }else if(type === null && res.chatid.includes('KEFU')){
                const { chatid, nickname, head } = res
                const PrologueMsgTips: MessageType = {
                    id: new Date().getTime() + '_local',
                    head,
                    nickname,
                    sendid: chatid,
                    time: timeFormater().format('YYYY-MM-DD HH:mm:ss'),
                    info: {
                        type: 'text',
                        value: '客服继续为您服务'
                    }
                }
                dispatch(setMessageList(PrologueMsgTips))
                scrolltoBottom && scrolltoBottom()
            }
            dispatch(setServiceInfo(res))
        },
        refreshDeps: [type]
    })
}

export default useLoadServeInfo